package com.etmvc.service.test;

import java.util.ArrayList;
import java.util.List;

import com.et.ar.ActiveRecordBase;
import com.et.ar.exception.ActiveRecordException;
import com.et.ar.exception.TransactionException;
import com.etmvc.model.test.User;

public class UserService {
	public List<User> findAll(){
		try {
//			return User.findAll(User.class);
			return User.findBySql(User.class,"select * from t_user");
		} catch (ActiveRecordException e) {
			e.printStackTrace();
		}
		return new ArrayList<User>();
	}

	public void add(User user) {
		try {
			user.save();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

	public void delete(int id) {
		try {
			User.find(User.class, id).destroy();
		} catch (ActiveRecordException e) {
		}
	}

	public void update(int id, String name, int age) {
		try {
			ActiveRecordBase.beginTransaction();
			User user = User.find(User.class, id);
			user.setName(name);
			user.setAge(age);
			user.update();
			ActiveRecordBase.commit();
		} catch (ActiveRecordException e) {
			try {
				ActiveRecordBase.rollback();
			} catch (TransactionException e1) {
				e1.printStackTrace();
			}
		}
		
	}

	public User findById(int id) {
		try {
			return User.find(User.class, id);
		} catch (ActiveRecordException e) {
			e.printStackTrace();
		}
		return null;
	}
}
